/**
 * 次卡套餐的model
 * @author: zxy
 * @update: 2016-03-14
 */

import {query, save} from '../services/timesCard';
import {message} from 'antd';
import {parse} from 'qs';

export default {

  namespace: 'timesCard',

  state: {
    total: 0,
    pageNumber: 1,
    loading: false,
    pageSize: 10,
    list: [],
    modalVisible: false,
    modalLoading: false,
    modalType: 'create',
    btnLoading: false,
    query: {},
    item: {},
    selectedRowKeys: [],
  },

  subscriptions: {
    setup({dispatch, history}) {
      history.listen(location => {
        // dispatch({
        //   // type: 'query',
        //   // payload: {
        //   //   pageNumber: 1,
        //   //   pageSize: 10,
        //   // }
        // });
      });
    }
  },

  effects: {
    *query({payload}, {call, put, select}){
      yield put({type: 'showLoading'});
      const querys = yield select(state => state.timesCard.query);
      const {data} = yield call(query, parse({...querys, ...payload}));
      if (data && data.success) {

        const {list, total, pageNumber, pageSize} = data.data.data;

        yield put({
          type: 'querySuccess',
          payload: {
            list,
            total,
            pageNumber,
            pageSize,
          },
        });
      } else {
        message.error(data && data.message || '数据获取失败');
      }
    },
    *'delete'({payload}, {call, put}) {
      yield put({type: 'showLoading'});
      const {pageNumber, pageSize} = payload;
      const {data} = yield call(remove, payload.id);
      if (data && data.success) {
        message.success('删除成功');
        yield put({
          type: 'query',
          payload: {
            pageNumber,
            pageSize,
          }
        });
      } else {
        message.error(data && data.message || '数据获取失败');
        yield put({type: 'hideLoading'});
      }
    },
    *create({payload}, {call, put}) {
      yield put({type: 'showBtnLoading'});
      const {formData, fn, pageNumber, pageSize} = payload;
      const {data} = yield call(create, formData);
      if (data && data.success) {
        fn()
        message.success('保存成功');
        yield put({type: 'hideModal'});
        yield put({type: 'hideBtnLoading'});
        yield put({
          type: 'query',
          payload: {
            pageNumber,
            pageSize,
          },
        });
      } else {
        yield put({type: 'hideBtnLoading'});
        message.error(data && data.message || '数据获取失败');
      }
    },
    *update({payload}, {select, call, put}) {
      yield put({type: 'showBtnLoading'});
      const {formData, fn, pageNumber, pageSize} = payload;
      const {data} = yield call(update, formData);
      if (data && data.success) {
        fn();
        message.success('保存成功');
        yield put({type: 'hideModal'});
        yield put({type: 'hideBtnLoading'});
        yield put({
          type: 'query',
          payload: {
            pageNumber,
            pageSize,
          },
        });
      } else {
        message.error(data && data.message || '数据获取失败');
        yield put({type: 'hideBtnLoading'});
      }
    },
    *add({payload}, {select, call, put}) {
      yield put({
        type: 'showModal',
        payload: {
          modalType: 'create',
          modalLoading: true,
        }
      });
    },
    *detail({payload}, {select, call, put}) {
      yield put({
        type: 'showModal',
        payload: {
          modalType: payload.modalType,
          modalLoading: true
        }
      });
      const {id} = payload;
      const {data} = yield call(detail, id);
      if (data && data.success) {
        const res = data.data;

        yield put({
          type: 'updateAction',
          payload: {
            item: res,
          },
        });
      } else {
        message.error(data && data.message || '数据获取失败');
      }
    },
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload, loading: false, selectedRowKeys: [] };
    },
    updateAction(state, action) {
      return { ...state, ...action.payload };
    },
    showLoading(state) {
      return {...state, loading: true};
    },
    hideLoading(state) {
      return {...state, loading: false};
    },
    showBtnLoading(state) {
      return {...state, btnLoading: true};
    },
    hideBtnLoading(state) {
      return {...state, btnLoading: false};
    },
    showModal(state, action) {
      return { ...state, ...action.payload, modalVisible: true, };
    },
    hideModal(state) {
      return { ...state, modalVisible: false};
    },
  },
};